﻿using System;
using System.Collections.Generic;
using WeiboPlus.Models;

namespace WeiboPlus.Service
{
    public abstract class MappingFactoryBase : IMappingFactory
    {
        protected SNSAccount _account;

        public SNSAccount Account
        {
            get
            {
                return _account;
            }
        }

        protected MappingFactoryBase(SNSAccount account)
        {
            _account = account;
            RegisterMapping();
        }

        protected IDictionary<string, IMapping> _mappings = new Dictionary<string, IMapping>();

        public virtual IMapping<T1, T2> GetMapping<T1, T2>()
        {
            var key = typeof(T1).Name +"|"+ typeof(T2).Name;
            if (_mappings.ContainsKey(key))
                return _mappings[key] as IMapping<T1, T2>;
            else
                throw new Exception("Type not register yet");
        }

        public virtual IMapping<T1, T2> GetMapping<T1, T2>(string key)
        {
            var keyStr = key+typeof(T1).Name + "|" + typeof(T2).Name;
            if (_mappings.ContainsKey(keyStr))
                return _mappings[keyStr] as IMapping<T1, T2>;
            else
                throw new Exception("Type not register yet");
        }

        protected abstract void RegisterMapping();
    }
}
